package com.wyd.utils;

import java.util.Collection;
import java.util.Map;

public final class ObjUtils {

    /**
     * 判断对象是否为空
     * <pre>
     * null                 = true
     * ""                   = true
     * empty Map            = true
     * empty Collection     = true
     * </pre>
     */
    public static boolean isEmpty(Object... objs) {
        if (objs == null || objs.length == 0) {
            return true;
        }
        for (Object obj : objs) {
            if (obj == null)
                return true;
            if ("".equals(obj.toString().trim()))
                return true;
            if (obj instanceof Collection) {
                Collection<?> collection = (Collection<?>) obj;
                return collection.isEmpty();
            }
            if (obj instanceof Map) {
                Map<?, ?> map = (Map<?, ?>) obj;
                return map.isEmpty();
            }
        }
        return false;
    }

    /**
     * 判断对象是否不为空
     * <pre>
     * null                 = false
     * ""                   = false
     * empty Map            = false
     * empty Collection     = false
     * </pre>
     */
    public static boolean isNotEmpty(Object... objs) {
        return !isEmpty(objs);
    }

    /**
     * 若对象为空则替换为目标对象
     */
    public static <T> T replaceIfEmpty(T checkObj, T replaceObj) {
        return isEmpty(checkObj) ? replaceObj : checkObj;
    }
}
